package edu.webarchitechture.bank.web.actions;

package edu.webarchitechture.bank.web.actions;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import edu.webarchitechture.bank.ejb3.domain.Account;
import edu.webarchitechture.bank.ejb3.services.AccountManager;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.List;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 * Action class to manage account related actions.
 *
 * 
 */
public class TransferMangerAction  extends ActionSupport implements Preparable {

    /** Transaction  bean.*/
    private AccountManager accManager;
    
    private List<Account> accounts;

    

    /**
     * This method will be executed before executing any of the action method in the action class.
     *
     * @throws Exception An exception
     */
    public void prepare() throws Exception {
       

        try {
            Context c = new InitialContext();
            accManager = (AccountManager) c.lookup("AccountManagerBean/local");
        } catch (NamingException ex) {
            System.out.println("NamingException Called");
        } catch (IllegalArgumentException ex) {
            System.out.println("IllegalArgumentException Called");
        }
    }

    public String showUserAccounts() {
        

        try {
            //todo specify the current customer id for getting actual values.
        	accounts = accManager.getAccountsOfCustomer(new Long(1));
        } catch (Exception e) {
            addActionError(e.getMessage());
            e.printStackTrace();
            return INPUT;
        }
        return SUCCESS;
    }
    
public String showServiceAccounts() {
        

        try {
            //todo specify the current customer id for getting actual values.
        	accounts = accManager.getServiceAccounts();
        } catch (Exception e) {
            addActionError(e.getMessage());
            e.printStackTrace();
            return INPUT;
        }
        return SUCCESS;
    }

public String validateAccount(Long accNo) {
    

    try {
        //todo specify the current customer id for getting actual values.
    	accounts = accManager.validateAccount(accNo);
    } catch (Exception e) {
        addActionError(e.getMessage());
        e.printStackTrace();
        return INPUT;
    }
    return SUCCESS;
}

    public String getToday() {
        Date today = new Date();
        DateFormat df = new SimpleDateFormat("dd/mm/yyyy");
        return df.format(today);
    }

   
}

